<?php
/**
 * 列表控制器
 */
class ListAction extends Action{
	public function index(){
		/*$sname=$_SERVER['REQUEST_URI'];
		if(S($sname)){
			$arr=S($sname);
		}else{
		$id=intval($_GET['id']);
		$db=M('category');
		$arr=$db->select();
		$ids=recursion($arr,0,4,$id);//找到改id的所有子类
		$ids[]=$id;
		$where=array('cid'=>array('IN',$ids));
		$goods=D('ListView');
		import('ORG.Util.Page');
		$count = $goods->where($where)->count();
		$page = new Page($count, 20);
		$limit = $page->firstRow . ',' . $page->listRows;
		$result = $goods->where($where)->order('time DESC,hot DESC')->limit($limit)->select();
		$pages=$page->show();
		$arr=array();
		$arr['result']=$result;
		$arr['pages']=$pages;
		S($sname,$arr,1);
		}*/
		$id=intval($_GET['id']);//分类的id
		$db=M('category');
		$arr=$db->select();
		//p($arr);
		$top=topid($arr,$id);//找到该分类的顶级父类
		//p($top);
		//返回顶级分类下的所有子类
		$childid=childid($arr,$top['id']);
		ksort($childid);
		//p($childid);
		$fulei=parentid($arr,$id);//找到该id的所有父类
		$ids=recursion($arr,0,4,$id);//找到该id的所有子类
		$ids[]=$id;
		//p($ids);
		//查找所有的商品的id
		$goods=M('goods');
		$gids=$goods->where(array('cid'=>array('IN',$ids)))->field(array('id'))->select();
		//p($gids);
		foreach($gids as $k=>$v){
			$gids[$k]=$v['id'];
		}
		//p($gids);
		//取得所有商品的品牌
		$brand_goods=D('ListView');
		$brand=$brand_goods->where(array('id'=>array('IN',$gids)))->select();
		$brd=array();
		foreach($brand as $k=>$v){
			$brd[]=$v['bid'];
		}
		$brd=array_unique($brd);
		//p($brd);
		$bname=M('brand');
		$barr=$bname->where(array('id'=>array('IN',$brd)))->field(array('id','name'))->select();
		//p($barr);
		//取出所用商品的所含属性
		$goods_attr=M('goods_attr');
		$attr=$goods_attr->where(array('gid'=>array('IN',$gids)))->field(array('id','value','aid'))->group('value')->select();
		//属性归类
		//p($attr);
		$attrs=array();
		foreach($attr as $k=>$v){
			$k=$v['aid'];
			$attrs[$k][]=array(
				'id'=>$v['id'],
				'value'=>$v['value']
				);
		}
		//p($_GET);
		//p($attrs);
		//重组数组，归到每个类型属性名称下
		$type_attr=M('type_attr');
		$values=array();
		foreach($attrs as $k=>$v){
			$type=$type_attr->where(array('id'=>$k))->getField('type');
			if($type) continue;
			$values[]=array(
				'name'=>$type_attr->where(array('id'=>$k))->getField('name'),
				'value'=>$v
				);	
		}
		//p($values);
		//定义过滤参数数组filter
		$filter=isset($_GET['filter'])? explode('_',$_GET['filter']) :array_fill(0,count($values),0);
		//取得筛选属性后的商品ID
		//p($filter);
		$db=M();
		$goodswhere=array();
		foreach($filter as $v){
			if($v){
				$sql="SELECT DISTINCT(a.`gid`) FROM `hd_goods_attr` a, `hd_goods_attr` b WHERE a.`value`=b.`value` AND b.`id`=".$v;
				$goodswhere[]=$db->query($sql);
			}
		}
		//echo $db->getLastSql($sql);
		//p($filter);
		//p($goodswhere);
		//重新筛选属性后的商品ID组成二维数组
		foreach($goodswhere as $k=>$v){
			foreach($v as $key=>$value){
				$goodswhere[$k][$key]=$value['gid'];
			}
			
		}
		//p($goodswhere);
		//组合查询SQL
		//$where=array('cid'=>array('IN',$ids));
		$sql='SELECT `id`,`name`,`sprice`,`listpic` FROM `hd_goods` WHERE';
		//组合where条件
		$where='`cid` IN ('.implode(',',$ids).')';
		if(is_array($goodswhere)){
			foreach($goodswhere as $v){
				$where.=' AND `id` IN ('.implode(',',$v).')';
			}
		}
		//选择的品牌id
		if(isset($_GET['bid'])){
			$where.=' AND `bid`='.(int)$_GET['bid'];
		}
		$filters=$_GET['filter'];
		//p($where);
		$sql.=$where;
		//获得排序条件
		$cd=(int)$_GET['cd'];
		switch($cd){
			case 0:
			$sql.=' order by `sprice` DESC';
			break;
			case 1:
			$sql.=' order by `click` DESC';
			break;
			case 2:
			$sql.=' order by `click`';
			break;
			case 3:
			$sql.=' order by `sprice`';
			break;
			case 4:
			$sql.=' order by `time`';
			break;
			default:
			$sql.=' order by `time` DESC';
		}
		//载入分页类
		import('ORG.Util.Page');
		//统计总条数用于分页
		$db=M('goods');
		$count=$db->where($where)->count();
		$page=new Page($count,20);
		//查询sql链接上分页LIMIT
		//$sql.='LIMIT'.$page->firstRow.','.$page->listRows;
		$sql .= ' LIMIT ' . $page->firstRow . ',' . $page->listRows;
		 //查询结果集
		$result=$db->query($sql);
		//echo $db->getLastSql();
		//p($result);
		//分配至页码
		$this->page=$page->show();
		//分配过滤数据
		$this->filter=$filter;
		//商品总数
		$this->count=$count;
		//分配属性筛选选项
		$this->values=$values;
		$this->barr=$barr;
		$this->filters=$filters;
		//分配所查询商品的结果
		$this->goods=$result;
		//分配所有的父类
		$this->fulei=$fulei;
		//某分类的顶级父类
		$this->top=$top;
		//某顶级分类下的所有子分类
		$this->childid=$childid;
		$this->display();
		//p($filter);
		//$get = $_GET['_URL_'];
		//p(filter_url($filter,0,0));
		//echo __APP__;
		//p($get);die;
	}
}
?>